package cn.admobiletop.adsuyi.adapter.gdt.loader;

import android.app.Activity;

import com.qq.e.ads.interstitial2.UnifiedInterstitialAD;

import cn.admobiletop.adsuyi.ad.ADSuyiInterstitialAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiInterstitialAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.InterstitialAdListener;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 插屏视频广告加载器
 * @date 2020/4/8
 */
public class InterstitialAdLoader implements ADSuyiAdapterLoader<ADSuyiInterstitialAd, ADSuyiInterstitialAdListener> {
    private InterstitialAdListener interstitialAdListener;
    private UnifiedInterstitialAD unifiedInterstitialAD;

    /**
     * 加载插屏广告
     * @param interstitialAd
     * @param adapterParams
     * @param adListener
     */
    @Override
    public void loadAd(ADSuyiInterstitialAd interstitialAd, ADSuyiAdapterParams adapterParams, ADSuyiInterstitialAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(interstitialAd)
                && adapterParams != null
                && adapterParams.getPlatform() != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId adSuyiPlatformPosId = adapterParams.getPlatformPosId();
            // 广告加载的Activity
            Activity activity = interstitialAd.getActivity();
            // 广告平台广告位ID
            String platformPosId = adSuyiPlatformPosId.getPlatformPosId();

            loadInterstitialAD(activity, platformPosId, adListener);
        }
    }

    /**
     * 加载广点通插屏广告
     * @param activity      广告加载广告的Activity
     * @param platformPosId 广告平台广告位ID
     * @param adListener    广告监听回调
     */
    private void loadInterstitialAD(Activity activity, String platformPosId, ADSuyiInterstitialAdListener adListener) {
        // 构造插屏广告监听类，实现广点通插屏广告回调接口
        interstitialAdListener = new InterstitialAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通UnifiedInterstitialAD对象，传入必要的参数
        unifiedInterstitialAD = new UnifiedInterstitialAD(activity
                , platformPosId
                , interstitialAdListener);

        // 向封装的插屏监听类中传入unifiedInterstitialAD，用于返回给用户进行插屏的播放
        interstitialAdListener.setUnifiedInterstitialAD(unifiedInterstitialAD);
        // 加载广点通插屏广告
        unifiedInterstitialAD.loadAD();
    }

    @Override
    public void onResumed() {
        // 目前不做处理
    }

    @Override
    public void onPaused() {
        // 目前不做处理
    }

    /**
     * ADSuyi广告对象被释放会调用该方法
     */
    @Override
    public void release() {
        if (unifiedInterstitialAD != null) {
            unifiedInterstitialAD.destroy();
            unifiedInterstitialAD = null;
        }
        if (interstitialAdListener != null) {
            interstitialAdListener.release();
            interstitialAdListener = null;
        }
    }
}
